/*
 * Copyright 2008 echo.khannedy@gmail.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * ["Let's ROCK with JAVA"]
 * 
 * ["echo.khannedy@gmail.com"]
 * ["http://eecchhoo.wordpress.com/"]
 */
package open.usu.awt.util;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.LinearGradientPaint;
import java.awt.Paint;

/**
 * Kumpulan fungsi untuk Paint
 * 
 * @author usu
 * @author <a href="mailto:echo.khannedy@gmail.com">echo.khannedy@gmail.com</a>
 * @see <a href="http://eecchhoo.wordpress.com/">http://eecchhoo.wordpress.com/</a>
 * @see <a href="http://biosmansa.wordpress.com/">http://biosmansa.wordpress.com/</a>
 * @see <a href="http://code.google.com/p/eglasspane/">http://code.google.com/p/eglasspane/</a>
 */
public class GradientUtilities {

    /**
     * Untuk membuat gradient DIAGONAL_DOWN yaitu gradient dari kiri atas ke kanan bawah
     */
    public static int DIAGONAL_DOWN = 33333;
    /**
     * Untuk membuat gradient DIAGONAL_UP yaitu gradient dari kiri bawah ke kanan atas
     */
    public static int DIAGONAL_UP = 44444;
    /**
     * Untuk membuat gradient HORIZONTAL yaitu gradient dari kiri ke kanan
     */
    public static int HORIZONTAL = 11111;
    /**
     * Untuk membuat gradient VERTICAL yaitu gradient dari atas ke bawah
     */
    public static int VERTICAL = 22222;
    private static Paint paint;

    /**
     * Membaut GradientPaint dari dua warna berdasarkan sebuah komponen
     * 
     * @param c1 warna satu
     * @param c2 warna dua
     * @param dimention dimensi
     * @param type tipe gradient, misal HORIZONTAL, VERTICAL, DIAGONAL_DOWN, DIAGONAL UP
     * @return warna gradient berdasarkan parameter
     */
    public static Paint getLinearGradient(final Color c1, final Color c2, final Dimension dimention,
            final int type) {
        paint = null;
        if (type == GradientUtilities.VERTICAL) {
            paint = new GradientPaint(0, 0, c1, 0, dimention.height, c2);
        } else if (type == GradientUtilities.DIAGONAL_DOWN) {
            paint = new GradientPaint(0, 0, c1, dimention.width, dimention.height, c2);
        } else if (type == GradientUtilities.DIAGONAL_UP) {
            paint = new GradientPaint(0, dimention.height, c1, dimention.width, 0, c2);
        } else {
            paint = new GradientPaint(0, 0, c1, dimention.width, 0, c2);
        }
        return paint;
    }

    /**
     * Membuat GradientPaint dari beberapa warna berdasarkan sebauh komponen dan berbentuk sesuai
     * tipe yang ditentukan
     * @param colors array warna
     * @param rect dimensi
     * @param type tipe gradient, seperti HORIZONTAL, VERTICAL, DIAGONAL_DOWN, DIAGONAL_UP
     * @return warna gradient berdasarkan parameter
     */
    public static Paint getLinearGradient(final Color[] colors, final Dimension rect, final int type) {
        paint = null;

        final float step = 1.0F / (colors.length - 1);
        final float[] fractions = new float[colors.length];

        float now = 0;
        for (int i = 0; i < colors.length; i++) {
            fractions[i] = now;
            now += step;
        }

        if (type == GradientUtilities.VERTICAL) {
            paint = new LinearGradientPaint(0, 0, 0, rect.width, fractions, colors);
        } else if (type == GradientUtilities.DIAGONAL_DOWN) {
            paint = new LinearGradientPaint(0, 0, rect.width, rect.height, fractions,
                    colors);
        } else if (type == GradientUtilities.DIAGONAL_UP) {
            paint = new LinearGradientPaint(0, rect.height, rect.width, 0, fractions,
                    colors);
        } else {
            paint = new LinearGradientPaint(0, 0, rect.width, 0, fractions, colors);
        }
        return paint;
    }

    private GradientUtilities() {
        // utility class
    }
}
